package P5.LoginForm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;

public class LoginDialog extends JDialog     {
    private boolean checkOk = false;
    private JTextField txtUserName=null;
    private JPasswordField txtPassword=null;
    private JButton btnLogin=null;
    private JButton btnCancel=null;

    public static boolean checkOK(){
        LoginDialog frm=new LoginDialog();
        boolean result=frm.isOK();
        frm.dispose();
        return result;
    }

    public boolean isOK() {
        return checkOk;
    }
    public LoginDialog() {
        setTitle("登录");
        setLayout(new BorderLayout());
        add(getBanner(),BorderLayout.NORTH);//添加登录图片
        add(getLoginPane(),BorderLayout.CENTER);//添加登录组件
        pack();
        setLocationRelativeTo(null);//窗口居中
        setModal(true);

        btnLogin.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String strUname=txtUserName.getText();
                String strPwd=txtPassword.getText();
                if(strUname.equals("zhangsan")&&strPwd.equals("123")){
                    JOptionPane.showMessageDialog(null,"登录成功"+new SimpleDateFormat("hh:mm:ss").format(e.getWhen()));
                }
            }
        });
//        txtUserName.addFocusListener(new FocusAdapter() {
//            @Override
//            public void focusGained(FocusEvent e) {
//                txtUserName.setText("俺有焦点啦");
//            }
//
//            @Override
//            public void focusLost(FocusEvent e) {
//                txtUserName.setText("焦点丢啦");
//            }
//        });
        setVisible(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }
    private JPanel getLoginPane() {
        JPanel panel=new JPanel();
        panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
        panel.add(Box.createGlue());
        panel.add(getLoginBox());
        panel.add(Box.createGlue());
        return panel;
    }

    private Box getLoginBox() {
        Box box=Box.createVerticalBox();
        Box boxUserName=Box.createHorizontalBox();
        Box boxPassword=Box.createHorizontalBox();
        JLabel lbluser=new JLabel("用户名:",JLabel.RIGHT);
        JLabel lblpwd=new JLabel("密码:",JLabel.RIGHT);
        lbluser.setPreferredSize(new Dimension(50,20));
        lblpwd.setPreferredSize(new Dimension(50,20));
        JTextField txtUserName=new JTextField(10);
        JPasswordField txtPassword=new JPasswordField(10);
        txtUserName.setMaximumSize(new Dimension(60,20));
        txtPassword.setMaximumSize(new Dimension(60,20));
        boxUserName.add(Box.createGlue());
        boxUserName.add(lbluser);
        boxUserName.add(Box.createHorizontalStrut(2));
        boxUserName.add(txtUserName);
        boxUserName.add(Box.createGlue());
        boxPassword.add(Box.createGlue());
        boxPassword.add(lblpwd);
        boxPassword.add(Box.createHorizontalStrut(2));
        boxPassword.add(txtPassword);
        boxPassword.add(Box.createGlue());

        JButton btnLogin=new JButton("确定");
        JButton btnCancel=new JButton("取消");

        Box boxButtons=Box.createHorizontalBox();
        boxButtons.add(Box.createGlue());
        boxButtons.add(Box.createHorizontalStrut(30));
        boxButtons.add(btnLogin);
        boxButtons.add(Box.createHorizontalStrut(5));
        boxButtons.add(btnCancel);
        boxButtons.add(Box.createGlue());


        box.add(Box.createVerticalStrut(20));
        box.add(boxUserName);
        box.add(Box.createVerticalStrut(10));
        box.add(boxPassword);
        box.add(Box.createVerticalStrut(10));
        box.add(boxButtons);
        box.add(Box.createVerticalStrut(20));

        //导出关键组件到类全局位置
        this.txtUserName=txtUserName;
        this.txtPassword=txtPassword;
        this.btnLogin=btnLogin;
        this.btnCancel=btnCancel;

        return box;
    }

    private JLabel getBanner() {
        return new JLabel(new ImageIcon("src/p5/LoginForm/banner.png"));
    }

}
